<?php
require_once ("secure_area.php");
require_once ("interfaces/idata_controller.php");
class Banks extends Secure_area implements iData_controller 
{
	function __construct()
	{
		parent::__construct('banks');
	}
	
	
	function index()
	{
		$sortable_data = $this->Bank->get_all();
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();		
		$data['manage_table']=get_banks_manage_table($sortable_data,$this);
		$this->load->view('banks/manage',$data);
	}
	
	function search()
	{
		$search=$this->input->post('search');
		$sortable_data = $this->Bank->get_all();
		$data_rows=get_banks_manage_table_data_rows($sortable_data,$this);
		echo $data_rows;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Bank->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	function get_row()
	{
		$item_id = $this->input->post('row_id');
		$all_banks = $this->Bank->get_info($item_id);
		$data_row=get_banks_data_row($all_banks,$this);
		echo $data_row;
	}
	
	function view($item_id=-1)
	{
		$data['bank_info']=$this->Bank->get_info($item_id);
		$this->load->view("banks/form",$data);
	}
	
	function save($item_id=-1)
	{
		$item_data = array(
		'name'=>$this->input->post('name')
		);
			
		if($this->Bank->save($item_data,$item_id))
		{				
			//New item
			if($item_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding').' '.
				$item_data['name'],'item_id'=>$item_data['item_id']));
				$item_id = $item_data['item_id'];
			}
			else //previous item
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_updating').' '.
				$item_data['name'],'item_id'=>$item_id));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			$item_data['name'],'item_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	
	function delete()
	{
		$items_to_delete=$this->input->post('ids');
		
		if($this->Bank->delete_list($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('tax_successful_deleted').' '.
			count($items_to_delete).' '.$this->lang->line('items_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 350;
	}	
}
?>